library(tidyverse)
library(tidylog)
library(lubridate)
library(xtable)

#GET CROSS-SECTIONAL CLIMATE TWEETS DATA
load("data/analysis/MPtweetsv2.Rdata")

climterms <- readRDS("data/output/climgenterms.rds")

#GET MP CLIMATE TWEETS
MPclimtweets <- MPtweets %>%
  filter(grepl(climterms,tweet, ignore.case = T))

#GET CROSS-SECTIONAL CROSS-WALK
MPcs <- read_csv("data/analysis/MP_cs_all.csv")
MPcw <- MPcs %>%
  select(about)

MPclimtweets_cw <- left_join(MPclimtweets, MPcw, by="about")

fff_events_dates <- read_csv("data/output/fff_events_dates.csv")
fff_events_dates <- fff_events_dates %>%
  mutate(obs = 1) %>%
  group_by(date, gss_code) %>%
  summarise(sum_fff_events = sum(obs))

MPctpdays <- left_join(MPclimtweets_cw, fff_events_dates, by = c("date", "gss_code"))

MPctpdays_filtered <- MPctpdays %>%
  filter(!is.na(sum_fff_events))

write_csv(MPctpdays_filtered, "data/output/MPclimprot_twts.csv")

# manually code
clim_twts_coded <- read_csv("data/output/MPclimprot_twts_coded.csv")

clim_twts_filtered <- clim_twts_coded %>%
  filter(type == "original")

clim_twts_filtered %>%
  group_by(attendance) %>%
  count()

clim_twts_filtered %>%
  filter(attendance==1) %>%
  group_by(date, full_name_value, party_value) %>%
  count() %>%
  group_by(party_value) %>%
  filter(party_value== "Labour"|party_value=="Labour (Co-op)") %>%
  count() %>%
  arrange(n)

clim_twts_filtered %>%
  filter(attendance==1) %>%
  group_by(date, full_name_value, party_value) %>%
  count() %>%
  group_by(party_value) %>%
  filter(party_value== "Labour"|party_value=="Labour (Co-op)") %>%
  distinct(full_name_value) %>%
  count() %>%
  arrange(n)

clim_twts_filtered %>%
  filter(attendance==1) %>%
  group_by(date, full_name_value, party_value) %>%
  count() %>%
  group_by(party_value) %>%
  filter(party_value== "Conservative") %>%
  count() %>%
  arrange(n)

clim_twts_filtered %>%
  filter(attendance==1) %>%
  group_by(date, full_name_value, party_value) %>%
  count() %>%
  group_by(party_value) %>%
  filter(party_value== "Conservative") %>%
  distinct(full_name_value) %>%
  count() %>%
  arrange(n)

mpprotcounts <- clim_twts_filtered %>%
  filter(attendance==1) %>%
  group_by(date, full_name_value, party_value) %>%
  count() %>%
  group_by(full_name_value, party_value) %>%
  count() %>%
  arrange(party_value, n)
  
x.big <- xtable(mpprotcounts, digits = 0)
print(x.big,  tabular.environment = "longtable", floating = F, include.rownames=FALSE)
